package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.Pay;
import com.ruoyi.system.mapper.PayMapper;
import com.ruoyi.system.order.Constants;
import com.ruoyi.system.order.WeiXinUtil;
import com.ruoyi.system.service.TestService;
import com.ruoyi.system.vo.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author : handsome boy
 * @date : 2024-12-16 20:03
 * @description :
 **/
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    PayMapper payMapper;

    @Override
    public PayResult payment(String orderId) throws Exception {
        Pay pay  = Pay.builder().
                notifyUrl(Constants.NOTIFY_URL)
                .channelId("bw")
                .payStatus(Long.valueOf(1))
                .payAmount(Long.valueOf(1))
                .asynUrl(Constants.NOTIFY_URL)
                .channelStatus(Long.valueOf(0))
                .channelName("八维产品")
                .merchandId(Constants.MCH_ID)
                .orderId(orderId)
                .paymentFee(Long.valueOf(0))
                .paymentJson("")
                .paymentStatus(Long.valueOf(0))
                .paymentMethodId("")
                .paymentTime(new Date())
                .privateKey("")
                .tradeType("微信")
                .merchandId("100001")
                .payAmount(Long.valueOf(1))
                .build();

        int i = this.payMapper.insertPay(pay);
        System.out.println(i);
        String s = WeiXinUtil.payOrder(orderId);

        if (StringUtils.isBlank(s)){
            return PayResult.builder().data("支付失败").status(2).build();
        }
        pay.setCodeUrl(s);
        pay.setPayStatus(Long.valueOf(2));
        System.out.println(s);
        this.payMapper.updatePay(pay);

        return PayResult.builder().data(s).status(1).build();
    }
}
